package com.hfmall.controller;


import com.hfmall.entity.*;
import com.hfmall.service.IHfGoodsService;
import com.hfmall.service.IHfOrderService;
import com.hfmall.service.*;
import com.hfmall.utils.CodeGenerator;
import com.hfmall.utils.ResponseState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liclo
 * @author gongkun_pc 添加 topOrder
 * @since 2021-08-12
 */
@RestController
@CrossOrigin
@RequestMapping("order")
public class HfOrderController {
    @Autowired
    private IHfOrderService orderService;

    @Autowired
    private IHfOrderitemService orderitemService;

    @Autowired
    private IHfGoodsService goodsService;

    @Autowired
    private IHfUserService userService;

    @Autowired
    private IHfGoodsinfoService goodsinfoService;

    @Autowired
    private IHfAddressService addressService;

    @PostMapping("list/{statusNo}")
    public ResponseCode<List<HfOrder>> list(@PathVariable String statusNo, @RequestBody HfUser user) {


        if (user==null){
            return new ResponseCode<>(ResponseState.FAIL, "fail");
        }

        HfOrder order = new HfOrder();
        order.setUserId(user.getId());
        order.setStatus(statusNo);
        if (statusNo.equals("0")){
            order.setStatus("");
        }

        List<HfOrder> orders = orderService.list(order);
        return new ResponseCode<>(orders);
    }


    @GetMapping("cancel/{orderId}")
    public ResponseCode<?> cancel(@PathVariable int orderId) {

        HfOrder order = new HfOrder();
        order.setId(orderId);
        order.setStatus("7");
        orderService.updateById(order);

        return new ResponseCode<>(ResponseState.SUCCESS);
    }

    @GetMapping("del/{orderId}")
    public ResponseCode<?> del(@PathVariable int orderId) {

        HfOrder order = new HfOrder();
        order.setId(orderId);
        order.setIsdel("y");
        orderService.updateById(order);

        return new ResponseCode<>(ResponseState.SUCCESS);
    }


    @GetMapping("confirm/{orderId}")
    public ResponseCode<?> confirm(@PathVariable int orderId) {
        HfOrder order = new HfOrder();
        order.setId(orderId);
        order.setStatus("4");
        orderService.updateById(order);
        return new ResponseCode<>(ResponseState.SUCCESS);
    }

    @GetMapping("getById/{orderId}")
    public ResponseCode<HfOrder> getById(@PathVariable int orderId){

       HfOrder order = orderService.getOne(orderId);

        return new ResponseCode<>(order);
    }


    @GetMapping("/pageList")
    public ResponseCode<?> list(Integer current,String statusNo, Integer size,Integer userId){
        if (current == null) {
            current = 1;
        }
        if (size == null) {
            size = 5;
        }
        return new ResponseCode<>(orderService.getPages(current,userId,statusNo,size));
    }

    @PostMapping("getCount")
    public ResponseCode<HfOrderCount> getCount(@RequestBody HfUser user){

      HfOrderCount count= orderService.getCount(user.getId());

        return new ResponseCode<>(count);
    }
    /*订单页面商品信息list*/
    @PostMapping("initGoods")
    public ResponseCode<List<UtilGoodsVO>> initGoods(@RequestBody UtilGoodsInfoVO info){
        List<UtilGoodsVO> goods=new ArrayList<>();
        List<UtilGoodsVO> utilGoodsVOList=new ArrayList<>();
        UtilGoodsVO goodsVO=new UtilGoodsVO();
        ArrayList<Integer> ids=info.getIds();
        ArrayList<Integer> nums=info.getNums();
        for (int i=0;i<ids.size();i++){
            String values="";
            utilGoodsVOList=goodsService.getInfoById(ids.get(i));
            for(UtilGoodsVO goodsV:utilGoodsVOList){
                goodsVO=goodsV;
                values=values+goodsV.getClassName()+":"+goodsV.getValue()+"  ";
            }
            goodsVO.setValues(values);
            goodsVO.setNum(nums.get(i));
            goods.add(goodsVO);
        }
        return new ResponseCode(goods);
    }
    /*提交订单*/
    @PostMapping("addOrder")
    public ResponseCode<Void> addOrder(@RequestBody UtilOrderInfoVO info){
        if(info.getUser().getId()==0){
            return new ResponseCode(403,"用户丢失了鸭,请重新登录");
        }
        HfUser user=info.getUser();
        ArrayList<Integer> ids=info.getIds();
        ArrayList<Integer> nums=info.getNums();
        HfOrder order=new HfOrder();
        //订单数据set订单号
        order.setOrderNo(CodeGenerator.getNo("NO"));
        String address="";
        //订单数据set详细地址
        HfAddress add=addressService.getById(info.getAddressId());
        address=add.getProvince()+" "+add.getCity()+" "+add.getArea()+" "+add.getDetail();
        order.setAddress(address);
        //订单数据set时间属性
        order.setCreateDate(CodeGenerator.getNowTime());
        //订单数据user属性
        order.setUserId(user.getId());
        order.setName(add.getRName());
        order.setPhone(add.getPhone());
        order.setStatus("1");
        order.setIsdel("n");
        //存放总价格
        BigDecimal sums=new BigDecimal("0");
        //存放小计数据
        List<BigDecimal> sum=new ArrayList<>();
        //计算总金额及存储小计数据
        for (int i = 0; i < nums.size(); i++) {
            HfGoods goods=goodsService.getById(ids.get(i));
            //数量Integer强转BigDecimal
            BigDecimal number=new BigDecimal(nums.get(i));
            //小计金额
            BigDecimal count=goods.getPrice().multiply(number);
            //list加入
            sum.add(count);
            //相加
            sums=sums.add(count);
        }
        //订单数据set总金额
        order.setPayMoney(sums);
        //如果是余额付款,修改用户余额值
        if("balance".equals(info.getPayType())){
            userService.updateMoney(user.getId(),user.getMoney().subtract(sums));
        }
        orderService.save(order);
        //添加订单详情
        for (int i=0;i<sum.size();i++){
            HfOrderitem orderitem=new HfOrderitem();
            orderitem.setName(goodsinfoService.getById(goodsService.getById(ids.get(i)).getGoodsinfoId()).getGoodsName());
            orderitem.setNo(goodsService.getById(ids.get(i)).getSku());
            orderitem.setPrice(goodsService.getById(ids.get(i)).getPrice());
            orderitem.setNumber(nums.get(i));
            orderitem.setSum(sum.get(i));
            orderitem.setOrderId(order.getId());
            String stock="";
            List<UtilGoodsVO> goodsVOS=goodsService.getInfoById(ids.get(i));
            for(UtilGoodsVO goodsV:goodsVOS){
                stock=stock+goodsV.getClassName()+":"+goodsV.getValue()+"  ";
            }
            orderitem.setStock(stock);
            orderitemService.save(orderitem);
        }
        return new ResponseCode().SUCCESS;
    }
    @GetMapping("list")
    public ResponseCode<List<HfOrder>> list(){
         return new ResponseCode<List<HfOrder>>(orderService.list());
    }
    @PostMapping("updates")
    public ResponseCode update(@RequestBody HfOrder order){
        orderService.updateByID(order);
        return ResponseCode.SUCCESS;
    }
    @PostMapping("del")
    public ResponseCode del(@RequestBody HfOrder order){
        orderService.removeById(order.getId());
        return ResponseCode.SUCCESS;
    }
    @PostMapping("select")
    @ResponseBody
    public ResponseCode<List<HfOrder>> selectByTime(@RequestBody HfOrder order){
        return new ResponseCode<List<HfOrder>>(orderService.getByCondition(order));
    }
//    @GetMapping("selectBy")
//    @ResponseBody
//    public ResponseCode<List<HfOrder>> selectBy(int status){
//        return new ResponseCode<List<HfOrder>>(orderService.selectByStatus(status));
//    }
}
